home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13341 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.0 KB

  1. Path: news.luc.edu!user
  2. From: VArase@varase.it.luc.edu (Verne Arase)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Turbo C beginner: string question? Please help!
  5. Date: Fri, 05 Apr 1996 22:55:42 -0600
  6. Organization: LUMC
  7. Message-ID: <AD8B556E9668E37D2@mcdiala13.it.luc.edu>
  8. References: <Pine.SOL.3.91.960403054553.29446A-100000@jove.acs.unt.edu> <4k00jn$bsk@nadine.teleport.com>
  9. NNTP-Posting-Host: 147.126.240.113
  10.  
  11. In article <4k00jn$bsk@nadine.teleport.com>,
  12. GHouck <hksys@teleport.com> wrote:
  13.  
  14.  >  for( i=0; i<strlen(string); i++ )        /* seq thru chars, tallying
  15. each */
  16.  >    ++chrCounts[(unsigned)string[i]];
  17.  
  18. Depending on the smarts of your compiler, this could get awfully expensive.
  19.  
  20. Best to pull strlen() out of your loop (and pop it into an intermediate
  21. variable) just in case.
  22.  
  23. Or better yet ...
  24.  
  25.    for (i=0; string[i]; i++)
  26.       ++chrCounts[(unsigned)string[i]];
  27.  
  28. Or my preference ...
  29.  
  30.    unsigned char *buf;
  31.    
  32.    for (buf=string; *buf; buf++)
  33.       ++chrCounts[*buf];
  34.  
  35.  
  36. ---
  37. The above are my own opinions, and not those of my employer.
  38.